package my_a10bbc.cn.xp.com.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import my_a10bbc.HistoryActivity;
import my_a10bbc.cn.xp.com.adapter.IIndexViewPagerAdapter;
import sdstcom.lbq.one.MainActivity;
import sdstcom.lbq.one.R;

/**
 * Created by Administrator on 2017/1/9.
 */
public class IindexFragment extends Fragment implements ViewPager.OnPageChangeListener,IIndexVpFragment.CallBacksss ,MainActivity.CallBack{
    public ViewPager vp;
    private ArrayList<IIndexVpFragment> list;
    private IIndexViewPagerAdapter adapter;
    private boolean misScrolled;
    private static ArrayList<String> types;

    private Toolbar tb;
    private TextView tv;

    public static IindexFragment getInstance(String type){

//        types = type;
        Bundle bundle=new Bundle();
        bundle.putString("list",type);
        IindexFragment fragment=new IindexFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=LayoutInflater.from(getActivity()).inflate(R.layout.frag_index,null,false);
        vp=(ViewPager)view.findViewById(R.id.vp_frag_index);
        tb=(Toolbar)view.findViewById(R.id.tb_fragment_index);
        tv=(TextView)view.findViewById(R.id.tv_fragment_index);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
//        init();
    }

    private void init() {

        list = new ArrayList<>();
////        ArrayList<String> data=getArguments().getStringArrayList("list");
        Log.e("aaaaa", "init"+types.size());
        for (int i = 0; i <types.size() ; i++) {
            IIndexVpFragment frag1=IIndexVpFragment.getInstance(types.get(i));
            frag1.getcallback(this);
            list.add(frag1);
      }
        list.add(IIndexVpFragment.getInstance(""));
        adapter = new IIndexViewPagerAdapter(getActivity().getSupportFragmentManager(), list);
        vp.setAdapter(adapter);
        vp.addOnPageChangeListener(this);

    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        Log.e("TS", "position:"+position+"positionOffset:"+positionOffset+",positionOffsetPixels:"+positionOffsetPixels );
//
        tv.setText("第"+(position+1)+"天");

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

        switch (state) {
            case ViewPager.SCROLL_STATE_DRAGGING:
                misScrolled = false;
                break;
            case ViewPager.SCROLL_STATE_IDLE:
                misScrolled = true;
                break;
            case ViewPager.SCROLL_STATE_SETTLING:
                if (vp.getCurrentItem() == vp.getAdapter().getCount() -1 && !misScrolled) {
//                    vp.setCurrentItem(8,false);
                    /**
                     * startActivityForResult 与Activity中 关联
                     * 执行CallBack的selection 方法
                     */
                    startActivityForResult(
                            new Intent(getActivity(), HistoryActivity.class),1900);
//                    overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
//                    adapter.notifyDataSetChanged();
                }

                misScrolled = true;
                break;
        }
    }


    @Override
    public void sendData(String s) {
        for (int i = 0; i < types.size(); i++) {
            if (types.get(i).equals(s)){
                Log.e("TS", "sendData: "+types.get(i));
                vp.setCurrentItem(i+1);
            }
        }


    }


    @Override
    public void SendList(ArrayList<String> l) {
        types=new ArrayList<>();
        types=l;
        init();
    }

    @Override
    public void setSelection() {
        vp.setCurrentItem(list.size() -2,false);
    }
}
